﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace Tapper
{
    public class Player : Character
    {
        internal bool m_usingTap;
        internal bool m_isMoving;
        internal TableState m_tableState;
        private KeyboardState m_keyState;
        private KeyboardState m_oldKeyState;
        
        public Player(World world, Texture2D texture, Vector2 pos, Vector2 vel)
            : base(world, texture, pos, vel)
        {                             
            m_oldKeyState     = Keyboard.GetState();            
        }

        protected override Vector2 GetPosX()
        {
            m_keyState  = Keyboard.GetState();
            var moveDir = new Vector2(0.0f, 0.0f);

            if (m_keyState.IsKeyDown(Keys.Right))
            {
                moveDir.X = 1.0f;
                if (m_usingTap)
                    moveDir.X = 0.0f;
            }           
            if (m_keyState.IsKeyDown(Keys.Left))
            {
                moveDir.X = -1.0f;           
            }            
            return moveDir;
        }
        
        protected override float GetPosY()
        {            
            m_keyState = Keyboard.GetState();
            if (m_keyState.IsKeyDown(Keys.Up) && !m_oldKeyState.IsKeyDown(Keys.Up))
            {
                // Retirar hardCoding
                switch (m_tableState)
                {
                    case TableState.OnTableOne:
                        m_pos.X       = 100.0f;
                        m_pos.Y       = 400.0f;
                        m_tableState = TableState.OnTableFour;
                        break;
                    case TableState.OnTableTwo:
                        m_pos.X       = 250.0f;
                        m_pos.Y       = m_pos.Y - 100.0f;
                        m_tableState = TableState.OnTableOne;
                        break;
                    case TableState.OnTableTree:
                        m_pos.X       = 200.0f;
                        m_pos.Y       = m_pos.Y - 100.0f;
                        m_tableState = TableState.OnTableTwo;
                        break;
                    case TableState.OnTableFour:
                        m_pos.X       = 150.0f;
                        m_pos.Y       = m_pos.Y - 100.0f;
                        m_tableState = TableState.OnTableTree;
                        break;
                }
                PosVelXtoDefaultWhenMoving();
            }
            if (m_keyState.IsKeyDown(Keys.Down) && !m_oldKeyState.IsKeyDown(Keys.Down))
            {
                switch (m_tableState)
                {
                    case TableState.OnTableOne:
                        m_pos.X       = 200.0f;
                        m_pos.Y       = m_pos.Y + 100.0f;
                        m_tableState = TableState.OnTableTwo;
                        break;
                    case TableState.OnTableTwo:
                        m_pos.X       = 150.0f;
                        m_pos.Y       = m_pos.Y + 100.0f;
                        m_tableState = TableState.OnTableTree;
                        break;
                    case TableState.OnTableTree:
                        m_pos.X       = 100.0f;
                        m_pos.Y       = m_pos.Y + 100.0f;
                        m_tableState = TableState.OnTableFour;
                        break;
                    case TableState.OnTableFour:
                        m_pos.X       = 250.0f;
                        m_pos.Y       = 100.0f;
                        m_tableState = TableState.OnTableOne;
                        break;
                }
                PosVelXtoDefaultWhenMoving();
            }                        
            return m_pos.Y;
        }        

        private Vector2 ResetPosXPlayer()
        {
            switch (m_tableState)
            {
                case TableState.OnTableOne:
                    m_pos.X = 250.0f;
                    break;
                case TableState.OnTableTwo:
                    m_pos.X = 200.0f;
                    break;
                case TableState.OnTableTree:
                    m_pos.X = 150.0f;
                    break;
                case TableState.OnTableFour:
                    m_pos.X = 100.0f;
                    break;
            }
            return m_pos;
        }

        private void PosVelXtoDefaultWhenMoving()
        {
            m_isMoving = true;
            m_vel      = Vector2.Zero;            
        }

        public override void Update(GameTime gametime)
        {
            if (m_world.m_gameState == GameState.OnGame)
            {
                if ((m_keyState.IsKeyDown(Keys.LeftControl)) && !m_oldKeyState.IsKeyDown(Keys.LeftControl))
                {
                    m_usingTap = true;
                    m_vel.X    = 0f;
                    m_pos      = ResetPosXPlayer();
                    m_world.AddEntity(new Mugs(m_world, m_world.Content.Load<Texture2D>("mugs"), (new Vector2(m_pos.X + 10.0f, m_pos.Y)), new Vector2(0.0f, 0.0f), this, null, MugState.OnPlayer, m_tableState));
                }
                if ((m_keyState.IsKeyUp(Keys.LeftControl)) && !m_oldKeyState.IsKeyUp(Keys.LeftControl))
                {
                    m_usingTap = false;
                }

                m_oldKeyState = m_keyState;
                base.Update(gametime);
            }
        }

        public override void Draw(GameTime gametime, SpriteBatch spritebatch)
        {
            const string output = "POSX PLAYER: ";
            var fontOrigin      = m_world.m_font.MeasureString(output) / 2;
            spritebatch.DrawString(m_world.m_font, output + m_pos.X, (new Vector2(100f, 50f)), Color.LightGreen, 0, fontOrigin, 1.0f, SpriteEffects.None, 0.5f);

            if (m_world.m_gameState == GameState.OnGameOver)
            {
                const string gameOverText = "GAME OVER";
                var fontGameOrigin = m_world.m_font.MeasureString(gameOverText) / 2;
                // ReSharper disable PossibleLossOfFraction
                 var fontPos = new Vector2(m_world.m_screenWidth / 2, m_world.m_screenHeight / 2);
                // ReSharper restore PossibleLossOfFraction
                 spritebatch.DrawString(m_world.m_font, gameOverText, fontPos, Color.LightGreen, 0, fontGameOrigin, 1.0f, SpriteEffects.None, 0.5f);
            }
            base.Draw(gametime, spritebatch);
        }
    }    
}
